#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include "xffmpeg.h"
#include <QMessageBox>
#include "xaudioplay.h"

static bool isPressSlider = false;
static bool isPlay = true;
#define PAUSE "QPushButton:hover{border-image: url(:/QPlayer/images/pause_hot.png);}"\
                    "QPushButton:!hover{border-image: url(:/QPlayer/images/pause_normal.png);}"

#define PLAY "QPushButton:hover{border-image: url(:/QPlayer/images/play_hot.png);}"\
                   "QPushButton:!hover{border-image: url(:/QPlayer/images/play_normal.png);}"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //openFile("test.mp4");
    startTimer(40);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent* e)
{
    int t = XFFmpeg::Get()->GetPTS();
    int h = t / 1000 / 60 / 60;
    int min = t / 1000 / 60;
    int sec = (t / 1000) % 60;
    char buf[1024] = {0};
    sprintf(buf, "%02d:%02d:%02d", h, min, sec);
    ui->playTime->setText(buf);

    if(t > 0)
    {
        float rate = t / 1000.0 / XFFmpeg::Get()->GetTotalMs();
        if(!isPressSlider)
            ui->playSlider->setValue(rate * 1000);
    }
}

void MainWindow::resizeEvent(QResizeEvent* e)
{
    ui->openGLWidget->resize(size());
}

void MainWindow::openFile(QString name)
{
    if(name.isEmpty())
        return;
    this->setWindowTitle(name);
    XFFmpeg* pff = XFFmpeg::Get();
    XAudioPlay* paudioplay = XAudioPlay::Get();
    int totalMs = pff->Open(name.toLocal8Bit());
    if(totalMs <= 0)
    {
        QMessageBox::information(this, "error", "file open failed!!!");
        return;
    }
    paudioplay->SetSampleRate(pff->GetSampleRate());
    paudioplay->SetSampleSize(pff->GetSampleSize());
    paudioplay->SetChannel(pff->GetChannel());
    paudioplay->Start();

    char buf[1024] = {0};
    int h = totalMs / 60 / 60;
    int min = totalMs / 60;
    int sec = totalMs % 60;
    sprintf(buf, "%02d:%02d:%02d", h, min, sec);
    ui->totalTime->setText(buf);
    isPlay = false;
    play();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    switch( QMessageBox::information(this,tr("提示"),tr("你确定退出该软件?"),tr("确定"), tr("取消"),0,1))
        {
        case 0:
            event->accept();
            break;
        case 1:
        default:
            event->ignore();
            break;
        }
}

void MainWindow::open()
{
    QString name = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择视频文件"));
    openFile(name);
}

void MainWindow::play()
{
    isPlay = !isPlay;
    XFFmpeg::Get()->SetIsPlay(isPlay);
    if(isPlay)
    {
        ui->playButton->setStyleSheet(PAUSE);
    }
    else
    {
        ui->playButton->setStyleSheet(PLAY);
    }
}

void MainWindow::sliderPress()
{
    isPressSlider = true;
}

void MainWindow::sliderRelease()
{
    isPressSlider = false;
    float pos = 0;
    pos = (float)ui->playSlider->value() / (ui->playSlider->maximum() + 1);
    XFFmpeg::Get()->Seek(pos);
}
